package se19team1e.util;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Vector;

import se19team1e.entity.JobAdvertDTO;

/**
 * 
 * 
 * @author arun
 * 
 */

public class ReflectionUtils {

	/**
	 * Static class to convert fields in a property file into a String which is used in the
	 * operations involving the files
	 * 
	 * @param object
	 *            Object that will be converted to string
	 * @return string that StringBuilder retrieves
	 * @exception if
	 *                Stringbuilder is unable to append fields
	 */
	public static String toString(Object object) {

		StringBuilder stringBuilder = new StringBuilder();

		Field fields[] = object.getClass().getDeclaredFields();

		for (Field field : fields) {
			field.setAccessible(true);
			try {
				stringBuilder.append(field.getName() + "=" + field.get(object)
						+ "\n");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return stringBuilder.toString();
	}

	/**
	 * A static class to get the fields in the header of a properties file
	 * 
	 * @param object
	 *            with a declared field
	 * @return vector of strings of headers
	 * @exception If
	 *                there is no header field
	 */
	public static Vector<String> getFields(Object object) {

		Vector<String> headerFields = new Vector<String>();

		Field fields[] = object.getClass().getDeclaredFields();

		for (Field field : fields) {
			field.setAccessible(true);
			try {
				headerFields.add(field.getName());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return headerFields;
	}

	/**
	 * To covert the header fields to upper case characters
	 * 
	 * @param object
	 *            the property file that will be parsed
	 * @return headerfields which are the first line of property file
	 * @exception if
	 *                there is a problem in adding a field name to be and
	 *                converting to upper case
	 */
	public static Vector<String> getFieldsInUpperCase(Object object) {

		
		Vector<String> headerFieldsTemp= getFields(object);
		Vector<String> headerFields= new Vector<String>();

		for (String header : headerFieldsTemp) {
			headerFields.add(header.toUpperCase());	
		}
		
		return headerFields;
	}

	/**
	 * A static utility function that will obtain the value given a field name
	 * 
	 * @param object
	 *            the name of the property file
	 * @param fieldName
	 *            the name of the field. It is the search key term.
	 * @return value which matches the search key
	 * @exception If
	 *                there is a problem in mapping a field and field name is
	 *                logged.
	 */
	public static String getValueFromFieldName(Object object, String fieldName) {

		Field dtoFields[] = object.getClass().getDeclaredFields();

		for (Field field : dtoFields) {
			field.setAccessible(true);

			if (!field.getName().equalsIgnoreCase(fieldName)) {
				continue;
			}
			try {

				if (field.getType() == Date.class) {
					return (String) DateUtils.getStringFromDate(
							field.get(object), Constants.DATE_FORMAT);
				} else if (field.getType() == boolean.class) {
					return String.valueOf(field.getBoolean(object));
				} else if (field.getType() == int.class) {
					return String.valueOf(field.getInt(object));
				} else if (field.getType() == double.class
						|| field.getType() == Double.class) {
					return String.valueOf(field.getDouble(object));
				} else {
					return String.valueOf(field.get(object));
				}

			} catch (Exception e) {
				System.out.println("Unable to map field : " + field.getName());
				e.printStackTrace();
			}
		}
		return "";

	}

	/**
	 * Static class that gets empty column data
	 * 
	 * @param columnHeader
	 *            A vector of string
	 * @return colomnData A vector of vectors
	 */
	public static Vector<Vector> getEmptyColumnData(Vector<String> columnHeader) {

		Vector<Vector> columnData = new Vector<Vector>();
		Vector<String> colDataTemp = new Vector<String>();

		for (String columnName : columnHeader) {
			colDataTemp.add(Constants.EMPTY_STRING);
		}

		columnData.add(colDataTemp);

		return columnData;
	}

	public static String valuesToString(Object object) {

		StringBuilder stringBuilder = new StringBuilder();

		Field fields[] = object.getClass().getDeclaredFields();

		for (Field field : fields) {
			field.setAccessible(true);
			try {
				stringBuilder.append(field.get(object)+Constants.SPACE);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return stringBuilder.toString();
	}
}
